--dofile(getinstalldir().."scripts/FileAccess.lua") 
--dofile(getinstalldir().."scripts/FindItems.lua") 

-- variabili personalizzabili
dropbag = UO.BackpackID
hotkey = "F5"
lootgold = true
lootgems = true


debug = false
hidelooted = false

--inizializzazione costanti e tavole
corpsetype = 8198
oldcorpses = {}
errmsg = {
  ["Something is standing in your way"] = true,
  ["That is out of sight."] = true,
  ["You must wait to perform another action."] = true,
  ["You can not pick that up."] = true,
  ["Target cannot be seen."] = true,
  [UO.CharName..": I can't reach that."] = true,
  }  
  
-- Liste loot
gems = {3855,3856,3857,3859,3861,3862,3864,3865,3873,3877,3878,3885}
gold = {3821}

-- INIZIO SCRIPT 
--[[
IgnoredCorpses = {} -- non modificare! necessaria
IgnoreItem(IgnoredCorpses,"Reset") -- non modificare! necessaria
IgnoreItem(IgnoredCorpses, UO.CharID, 1) -- mio pg

function FindCorpse(range,necrocorpse)
f = ScanItems(false,{Type=corpsetype},{Dist={1,2,3} , ID=IgnoredCorpses.ID})
--if debug then print(tostring(#f) ..  "trovati" ) end
if #f == 0 and debug then 
   UO.SysMessage ( "Trovato niente" , 233)
   timestamp(ms) 
   return 
end
return () 
end ]]--    




--presa dal choosy autolooter, non mi piace, DA RIFARE
--[[Features: debug
    Parameter: range = 'number'
    Return: true at first matching item
            false if nothing found
]]--

function buildlootlist ()
lootlist = {}
if lootgold then
   for k,v in pairs(gold) do
      table.insert(lootlist,v)
      end
end
if lootgems then
    for k,v in pairs(gems) do
      table.insert(lootlist,v)
      end
    end
end

function FindCorpse(range)
if debug then
   print ("==============FindCorpse=============")
   print (#oldcorpses .. " corpse ignorati")
   for cs = 1 , #oldcorpses do
      print (oldcorpses[cs])
   end
end
  local i, c, dx, dy
  for i = 0,UO.ScanItems(true)-1 do
    local id,type,kind,contid,x,y,z,stack,rep,col = UO.GetItem(i)
    if type == 8198 then
      local skip = false
      for c = 1,#oldcorpses do
        if id == oldcorpses[c] then
          skip = true
        end
      end
      if not skip then
        dx = math.abs(x-UO.CharPosX)
        dy = math.abs(y-UO.CharPosY)
        if dy < range and dx < range then
          corpse = id
          if debug then
               print ("==============OpenCorpse=============")
               print (corpse .. " trovato")
           end
          return true
        end
      end
    end
  end 
  return false  
end


--[[Parameter needed: global variable corpse = 'ID'
    Returns: false if timed out or error occurs   
             true and sets variables if success
             public variables: ccX
                               ccY
                               corpseCont
]]--

function OpenCorpse()
  UO.LObjectID = corpse
  local timeout = getticks() + 2000
  local tempCont = UO.ContID
  UO.Macro(17, 0, "")
  wait (100)
  repeat 
    if checkMsg(timeout - getticks()) then
      print("OpenCorpse function error: not possible to complete")
      return false
    end
    if corpsetype == UO.ContType and corpse == UO.ContID then
     corpseCont = UO.ContID
     ccX = UO.ContPosX + 75
     ccY = UO.ContPosY + 75
     return true
    end
    wait(5)
  until timeout < getticks()
return false
end

--[[Parameter: timeout = 'number'
    return: true if message is found
            false if nothing
]]--

function checkMsg(timeout)
  local ref = 0
  local i, jstr, col, cnt
  ref,cnt = UO.ScanJournal(ref)
  repeat
    wait(1)
    ref,cnt = UO.ScanJournal(ref)
  until cnt > 0 or timeout < getticks()
  if cnt < 1 then
    return false
  end
  for i = cnt-1,0,-1 do
    jstr,col = UO.GetJournal(i)
    if errmsg[jstr] then
      print(jstr)
      return true
    end
  end
return false
end

function CloseCorpse()
  while UO.ContID == corpseCont do
    UO.Click(ccX,ccY,false,true,true,false)
    wait (20)
  end
  table.insert(oldcorpses,corpse)
  UO.HideItem(corpse)
end

function dropitem(it,amt,bag)
  UO.Drag(it,amt)
  wait(100)
  repeat
  UO.DropC(dropbag)
  until not checkMsg(2000) 
  wait(210)
    --print("drag item error")
    --return false
  --end
  return true
end

function CheckLoot()
  lootTable = {}
  itemAmt = {}
  local i, j, added
  for i = 0,UO.ScanItems(true)-1 do
    local id,type,kind,contid,x,y,z,stack,rep,col = UO.GetItem(i)
    added = false
    for j = 1,#lootlist do
      if type == lootlist[j] and contid == corpseCont then
        table.insert(lootTable,id)
        table.insert(itemAmt,stack)
        added = true
      end
    end
    --[[if not added and contid == corpseCont then
      if evaluate(id) > minvalue then
        table.insert(lootTable,id)
        table.insert(itemAmt,1)
      end
    end 
    ]]-- 
  end
  if #lootTable == 0 or #lootTable == nil then
    if hidelooted then CloseCorpse () end
    return false
  end
  return true
end

LootCorpse = function ()
  local i,timeout
  timeout = getticks() + 20000 
  CheckLoot ()
  repeat  
  for i = 1, #lootTable do
    UO.SysMessage ( "looting " .. i .. "of " .. #lootTable , 233)
    if not dropitem(lootTable[i],itemAmt[i],lootbag) then
      print ("error looting?")
      return false
    end
  end
  until not CheckLoot () 
  return true
end

timestamp = function(ms)
--UO.SysMessage ("Operazione completata in " .. getticks() - ms .. " millisecondi" , 51)
print("Operazione completata in " .. getticks() - ms .. " millisecondi") 
end

function main()
   if UO.Weight >= maxweight - 40 then UO.SysMessage ( "Nearly overweight! Please unload before continuing looting." , 233) return false end
   if FindCorpse (3) then
      timer = getticks()
      if OpenCorpse () then
        if LootCorpse () then
          UO.SysMessage ( "looted in " .. getticks() - timer .. "ms" , 233)  
          if hidelooted then CloseCorpse () end    
        end
      else
          UO.SysMessage ( "looted in " .. getticks() - timer .. "ms" , 233)  
      end
   end
end

isHuman = function ()
return (UO.CharType == 400 or UO.CharType == 401 or UO.CharType == 402 or UO.CharType == 403 )
end

--start executing
if isHuman then maxweight = UO.MaxWeight + 60 else maxweight = UO.MaxWeight end 
buildlootlist()
repeat 
if getkey(hotkey) then
  main()
  wait(600)
  end
wait (5)
until false == true